package com.example.bishe.controller.Jxbm;

import com.alibaba.fastjson.JSON;
import com.example.bishe.entity.Bzks_yx;
import com.example.bishe.entity.Jxbm;
import com.example.bishe.service.Bzks_yxService;
import com.example.bishe.service.JxbmService;
import com.example.bishe.service.impl.Bzks_yxServiceImpl;
import com.example.bishe.service.impl.JxbmServiceImpl;
import com.example.bishe.toolclass.CUD;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author yws
 * @Date 2022/4/4 16:23
 */
@RestController
@RequestMapping("/jxbm")
public class JxbmController {
    @Resource
    CUD CUD;
    @Autowired
    JxbmService jxbm_ = new JxbmServiceImpl();
    @Autowired
    Bzks_yxService yxService = new Bzks_yxServiceImpl();
    @PostMapping("/addjxbm")
    public String addjxbm(@RequestBody Jxbm jxbm) {
        int errcode = 0;//错误代码
        String errmsg =" ";//错误描述
        int id;//返回创建成功的id
        Jxbm jxBm = new Jxbm(); jxBm.setDm(jxbm.getDm());
        Jxbm[] jxbms = jxbm_.selectJxbm(jxBm);
        Bzks_yx bzks_yx = new Bzks_yx();bzks_yx.setDm(jxbm.getLs());
        Bzks_yx[] bzks_yxes = yxService.select_yx(bzks_yx);
        HashMap value = null;
        if(jxbm.getWx_parentid()!=0&&jxbm.getWx_id()!=0)
            errmsg+="不能直接指定上级部门微信id,请通过隶属学院代码进行添加，";
        if(jxbm.getDm()==null)
            errmsg+="请求中没有教学部门代码,";
        if (jxbm.getBmname()==null)
            errmsg+="请求中没有教学部门名称，";
        if (jxbm.getLs()==null)
            errmsg+="请求中没有教学部门隶属学院代码，";
        if(errmsg.length()>1){
            errcode = 1201;
            errmsg = errmsg.substring(1, errmsg.length() - 1) + "。";
        }else if(jxbms.length==0){/*等于零表示之前不存在对应的单位代码，可以进行使用*/
            if(bzks_yxes.length!=0){
                bzks_yx = bzks_yxes[0];
                value = CUD.add(jxbm.getBmname(),
                        bzks_yx.getWx_id()+"",
                        jxbm.getWx_id()!=0?jxbm.getWx_id()+"":"");
                if (value.get("errcode").equals("0")) {
                    errcode = 0;
                    //可以插入数据库操作
                    jxbm.setWx_id(Integer.parseInt((String) value.get("id")));
                    jxbm.setWx_parentid(bzks_yx.getWx_id());
                    jxbm_.addJxbm(jxbm);
                    errmsg += "无错误。";
                    errmsg = errmsg.substring(1, errmsg.length());
                }else {
                    errcode = 1201;
                    errmsg+= CUD.addpan(value);
                }
            }else {
                errcode = 1201;
                errmsg += "没有找到对应的学院信息。";
            }
        }else {
            errcode = 1201;
            errmsg += "你所创建的专业代码已经存在。";
            errmsg = errmsg.substring(1, errmsg.length());
        }
        Map<String, String> map = new HashMap();
        map.put("errcode", String.valueOf(errcode));
        map.put("errmsg", errmsg);
        if (jxbm.getBmname() == null) {
            map.put("mingcheng", "null");
        } else {
            map.put("mingcheng", jxbm.getBmname());
        }
        String s = JSON.toJSONString(map);
        return s;
    }

    @PostMapping("/deletejxbm")
    public String deletejxbm(@RequestBody Jxbm jxbm) {
        int errcode = 0;//错误代码
        String errmsg = "";//错误描述
        if (jxbm.getDm() == null) {
            errcode = 1202;
            errmsg = "请传入你要删除的教学部门代码";
        }else {
            Jxbm jxBm = new Jxbm();
            jxBm.setDm(jxbm.getDm());
            Jxbm[] jxbms = jxbm_.selectJxbm(jxBm);
            if(jxbms.length!=0){
                jxBm=jxbms[0];
                HashMap<String, String> value = CUD.delete(jxBm.getWx_id()+"");
                if (value.get("errcode").equals("0")) {
                    jxbm_.deleteJxbm(jxBm);
                    errmsg = "已删除对应的教学部门";
                } else {
                    errmsg+= CUD.deletepan(value);
                    errcode=1202;
                }
            }else {
                errcode = 1202;
                errmsg = "没有找到你想要删除的教学部门";
            }
        }
        Map map = new HashMap();
        map.put("errcode", String.valueOf(errcode));
        map.put("errmsg", errmsg);
        String s = JSON.toJSONString(map);
        return s;
    }

    @PostMapping("/updatejxbm")
    public String updatejxbm(@RequestBody Jxbm jxbm) {
        int errcode = 0;//错误代码
        String errmsg = "";//错误描述
        if(jxbm.getWx_id()!=0||jxbm.getWx_parentid()!=0){
            errcode = 1403;
            errmsg = "不能修改教学部门的微信id，父部门id请修改部门的隶属学院代码";
        }
        if (jxbm.getDm() == null) {
            errcode = 1403;
            errmsg = "请传入你要修改的教学部门代码";
        }else {
            Jxbm jxBm = new Jxbm();
            jxBm.setDm(jxbm.getDm());
            Jxbm[] jxbms = jxbm_.selectJxbm(jxBm);
            if(jxbms.length!=0){
                jxBm = jxbms[0];/*找到的教学部门信息，之前的信息*/
                if(jxbm.getLs()!=null){
                    Bzks_yx bzksYx = new Bzks_yx();
                    bzksYx.setDm(jxbm.getLs());
                    Bzks_yx[] bzks_yxes = yxService.select_yx(bzksYx);
                    if (bzks_yxes.length != 0) {
                        bzksYx = bzks_yxes[0];
                        HashMap<String, String> value = CUD.update(jxBm.getWx_id()+"",
                                jxbm.getBmname(),
                                bzksYx.getWx_id()+"");
                        if(value.get("errcode").equals("0")){
                            errmsg+="已修改对应信息";
                            jxbm.setWx_id(jxBm.getWx_id());
                            jxbm.setWx_parentid(bzksYx.getWx_id());
                            jxbm_.queryJxbm(jxbm);
                        }else {
                            errcode=1403;
                            errmsg+= CUD.updatepan(value);
                        }
                    }else {
                        errcode = 1403;
                        errmsg = "你要修改的院系信息不存在";
                    }
                }else {
                    HashMap<String, String> value = CUD.update(jxBm.getWx_id()+"",
                            jxbm.getBmname(),
                            jxBm.getWx_parentid()+"");
                    if(value.get("errcode").equals("0")){
                        errmsg+="已修改对应信息";
                        jxbm.setWx_id(jxBm.getWx_id());
                        jxbm.setWx_parentid(jxBm.getWx_parentid());
                        jxbm_.queryJxbm(jxbm);
                    }else {
                        errcode=1403;
                        errmsg+= CUD.updatepan(value);
                    }
                }

            }else {
                errcode = 1403;
                errmsg = "没有找到你要修改的教学部门";
            }
        }
        Map map = new HashMap();
        map.put("errcode", String.valueOf(errcode));
        map.put("errmsg", errmsg);
        String s = JSON.toJSONString(map);
        return s;
    }

    @PostMapping("/selectjxbm")
    public String selectjxbm(@RequestBody Jxbm jxbm) {
        Jxbm[] jxbms = jxbm_.selectJxbm(jxbm);
        List<Jxbm> list = new ArrayList<>();
        for (int i = 0; i < jxbms.length; i++) {
            list.add(jxbms[i]);
        }
        Map map = new HashMap();
        map.put("Jxbm", list);
        String s = JSON.toJSONString(map);
        return s;
    }
}
